<?php

namespace app\common\model\banxi\core;

use app\common\model\banxi\trainer\Trainer;
use app\common\model\common\BaseModel;
use app\common\service\trainer\TrainerMonthSalaryDetailService;
use think\model\concern\SoftDelete;

/**
 * 教练
 */
class CoreTrainerMonthSalary extends BaseModel
{

    use SoftDelete;

    protected $name = 'banxi_core_trainer_month_salary';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
    ];
    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'trainerId' => 'string',
        'totalAmount' => 'float',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------


    //-----------------------------------------------------------------------------------
    //｜                                   处理办法                                       ｜
    //-----------------------------------------------------------------------------------

    public function getByTrainerAndMonth($trainerId, $month)
    {
        return $this->where(['trainerId' => $trainerId, 'month' => $month])->find();
    }

    public function getListPage(array $where, string $page = "1", int $Limit = 10)
    {
        return $this->with([
            'trainer' => function ($query) {
                $query->field('id,name,account');
            }])->where($where)->paginate([
            'page' => $page,
            'list_rows' => $Limit
        ]);
    }

    public function getInfo(array $where)
    {
        $info = $this->with([
            'trainer' => function ($query) {
                $query->field('id,name,account');
            }])->where($where)->find();
        if ($info) {
            $trainerMonthSalaryDetailService = new TrainerMonthSalaryDetailService();
            $detailList = $trainerMonthSalaryDetailService->listDetailByMonthSalaryId($info['id']);
            $info['detailList'] = $detailList ?? [];
        }
        return $info;
    }

    public function getExportExcel()
    {
        return $this->with([
            'trainer' => function ($query) {
                $query->field('id,name,account');
            }])->select();
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------
    public function trainer()
    {
        return $this->hasOne(Trainer::class, 'id', 'trainerId');
    }

}